package com.ti.demo.mcp;

import io.modelcontextprotocol.server.McpSyncServerExchange;
import io.modelcontextprotocol.spec.McpSchema;
import org.springaicommunity.mcp.annotation.McpResource;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: @McpResource
 * The @McpResource annotation provides access to resources via URI templates.
 * @Author Triagen
 * @Date 2025/10/30 13:24
 */
@Component
public class McpResources {

    // 使用ConcurrentHashMap保证线程安全
    private final Map<String, String> configData = new ConcurrentHashMap<>();

    // 模拟用户数据
    private final Map<String, String> userProfiles = Map.of(
            "zhangsan", "{\"username\":\"zhangsan\",\"name\":\"张三\",\"email\":\"zhangsan@example.com\",\"role\":\"admin\"}",
            "lisi", "{\"username\":\"lisi\",\"name\":\"李四\",\"email\":\"lisi@example.com\",\"role\":\"user\"}",
            "wangwu", "{\"username\":\"wangwu\",\"name\":\"王五\",\"email\":\"wangwu@example.com\",\"role\":\"user\"}"
    );

    private final Map<String, String> staticDataCache = new ConcurrentHashMap<>();

    public McpResources() {
        // 初始化一些默认配置数据
        initializeDefaultConfig();
    }

    private void initializeDefaultConfig() {
        configData.put("app.name", "MySpringAIApp");
        configData.put("app.version", "1.0.0");
        configData.put("api.timeout", "30000");
        configData.put("max.connections", "100");
        configData.put("cache.enabled", "true");

        staticDataCache.put("welcome", "Hello, World! This is the welcome message.");
        staticDataCache.put("status", "All systems are operational.");
        staticDataCache.put("config:timeout", "30000");
    }

    // Basic Usage
    @McpResource(
            uri = "config://{key}",
            name = "Configuration",
            description = "Provides configuration data")
    public String getConfig(String key) {
        return configData.get(key);
    }


    // With ReadResourceResult
    @McpResource(
            uri = "user-profile://{username}",
            name = "User Profile",
            description = "Provides user profile information")
    public McpSchema.ReadResourceResult getUserProfile(String username) {
        String profileData = loadUserProfile(username);

        return new McpSchema.ReadResourceResult(List.of(
                new McpSchema.TextResourceContents(
                        "user-profile://" + username,
                        "application/json",
                        profileData)
        ));
    }

    private String loadUserProfile(String username) {

        String profile = userProfiles.get(username);
        if (profile == null) {
            throw new IllegalArgumentException("User not found: " + username);
        }
        return profile;
    }


    // With Server Exchange
    @McpResource(
            uri = "data://{id}",
            name = "Data Resource",
            description = "Resource with server context")
    public McpSchema.ReadResourceResult getData(
            McpSyncServerExchange exchange,
            String id) {

        exchange.loggingNotification(McpSchema.LoggingMessageNotification.builder()
                .level(McpSchema.LoggingLevel.INFO)
                .data("Accessing resource: " + id)
                .build());

        String data = fetchData(id);

        return new McpSchema.ReadResourceResult(List.of(
                new McpSchema.TextResourceContents("data://" + id, "text/plain", data)
        ));
    }

    /**
     * 根据数据标识符 ID 获取数据。
     * 此处提供了多种实现思路，请根据您的需求选择一种或组合使用。
     *
     * @param id 资源标识符
     * @return 获取到的数据字符串
     */
    private String fetchData(String id) {
        return staticDataCache.get(id);
    }
}
